package com.gem.netty.message;

import com.gem.factory.room.RoomFactory;
import com.gem.factory.user.UserFactory;
import com.gem.netty.entity.ClientContent;
import com.gem.netty.entity.Content;
import com.gem.netty.session.GroupSession;
import com.gem.netty.session.GroupSessionFactory;
import com.gem.utils.R;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

import java.util.HashSet;
import java.util.List;

/**
 * Author: Xhy
 * CreateTime: 2022-09-21 13:14
 *
 * 创建房间 废弃
 */
@Deprecated
public class CreateRoomMessage implements Message{

    @Override
    public void handler(ClientContent content) {
        String userId = content.getSendId();
        String roomId = content.getRoomId();
        GroupSession groupSession = GroupSessionFactory.getGroupSession();
        HashSet<String> set = new HashSet<>();
        set.add(userId);
        groupSession.createGroup(roomId,set);
        // 创建房间基本信息
        Content c = content.getContent();
        R r = RoomFactory.createRoom(c.getRoomName(), userId);
        if (!r.getState()){
            content.getCtx().writeAndFlush(new TextWebSocketFrame(r.type(content.getType()).toString()));
            return;
        }

        List<Channel> channels = groupSession.getMembersChannel(roomId);
        for (Channel channel : channels) {
            channel.writeAndFlush(UserFactory.get(userId)+" 玩家创建了房间");
        }
    }

}
